package com.zoe.onelink.feign.auth.token.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.zoe.onelink.util.OnelinkContextHolder;
import com.zoe.onelink.feign.auth.token.FeignTokenEncryptor;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-05-12
 */
public class FeignAesAuthTokenEncryptor implements FeignTokenEncryptor {

    private final AES aes;

    public FeignAesAuthTokenEncryptor(String secretKey) {
        Assert.notNull(secretKey, "[FEIGN-TOKEN] 密钥不能为空");
        this.aes = SecureUtil.aes(secretKey.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String encrypt(Object... param) {
        Map<String, Object> authMap = OnelinkContextHolder.getCopiedMap();
        return this.aes.encryptBase64(JSON.toJSONString(authMap));
    }

    @Override
    public String decrypt(String token) {
        return this.aes.decryptStr(token);
    }
}
